home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / LISTINGS / V_12_11 / ALLISON / EXHAUST4.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-04  |  710 b   |  44 lines

  1. LISTING 20 - Implements an xalloc handler
  2. // exhaust4.cpp
  3. #include <stdio.h>
  4. #include <except.h>
  5.  
  6. main()
  7. {
  8.     double *dp;
  9.  
  10.     try
  11.     {
  12.         for (int i = 0; ; ++i)
  13.         {
  14.             dp = new double[100];
  15.             if ((i+1)%10 == 0)
  16.                 printf("%d allocations\n",i+1);
  17.         }
  18.     }
  19.     catch(xalloc)
  20.     {
  21.         delete [] dp;
  22.         puts("bad_alloc exception");
  23.         return 1;
  24.     }
  25.     catch(...)
  26.     {
  27.         delete [] dp;
  28.         puts("Something else happened");
  29.         return 1;
  30.     }
  31. }
  32.  
  33. /* Output:
  34. 10 allocations
  35. 20 allocations
  36. 30 allocations
  37. 40 allocations
  38. 50 allocations
  39. 60 allocations
  40. 70 allocations
  41. bad_alloc exception
  42. */
  43.  
  44.